
#import "CTWBProgress.h"

#import <UIKit/UIKit.h>

#define progressBorderWidth 2

@interface progressSubView : UIView

//进度条背景图片
@property(retain, nonatomic) UIImageView *trackView;

//进图条填充图片
@property(retain, nonatomic) UIImageView *progressView;

//
@property(retain, nonatomic) UILabel *lab;

//进度
@property(assign, nonatomic) CGFloat progress;

@end

@implementation progressSubView

//当你通过代码创建控件就会调用这个方法
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupWithFrame:frame];
    }
    return self;
}

//当你通过storyboared或者xib中创建控件就会调用这个方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupWithFrame:self.frame];
    }
    return self;
}

- (instancetype)setupWithFrame:(CGRect)frame {
    self.frame = frame;

    [self config:frame];

    return self;
}

- (void)drawRect:(CGRect)rect {
}

- (void)setNeedsDisplayInRect:(CGRect)rect {
}

- (void)config:(CGRect)frame {
    self.backgroundColor = [UIColor clearColor];
    self.layer.borderWidth = progressBorderWidth;
    self.layer.borderColor = UIColor.whiteColor.CGColor;
    self.layer.cornerRadius = self.frame.size.height / 2;
    self.layer.masksToBounds = YES;

    _progress = 0.0;

    //背景图像
    self.trackView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);

    //填充图像
    self.progressView.frame = CGRectMake(0 - frame.size.width, 0, frame.size.width, frame.size.height);

    //
    self.lab.frame = CGRectMake(0, 0, frame.size.width - 5, frame.size.height);

    //_trackView.layer.cornerRadius=self.layer.cornerRadius;
    //_progressView.layer.cornerRadius=self.layer.cornerRadius;
    [self setProgress:_progress];
}

#pragma mark-- (懒加载)

- (UIImageView *)trackView {
    if (!_trackView) {
        //背景图像
        _trackView = [[UIImageView alloc] init];
        //[_trackView setImage:[UIImage imageNamed:@"1.png"]];
        _trackView.backgroundColor = UIColor.clearColor;
        //当前view的主要作用是将出界了的_progressView剪切掉，所以需将clipsToBounds设置为YES
        _trackView.clipsToBounds = YES;
        [self addSubview:_trackView];
    }
    return _trackView;
}

- (UIImageView *)progressView {
    if (!_progressView) {
        //填充图像
        _progressView = [[UIImageView alloc] init];
        //[_progressView setImage:[UIImage imageNamed:@"2.png"]];
        _progressView.backgroundColor = [UIColor colorWithRed:198 / 255.0 green:167 / 255.0 blue:132 / 255.0 alpha:1];
        _progressView.clipsToBounds = YES;
        [self.trackView addSubview:_progressView];
    }
    return _progressView;
}

- (UILabel *)lab {
    if (!_lab) {
        //
        _lab = [[UILabel alloc] init];
        _lab.backgroundColor = UIColor.clearColor;
        //_lab.textColor = UIColor.whiteColor;
        _lab.textColor = [UIColor colorWithRed:0 green:0.04 blue:0.09 alpha:0.4];
        _lab.textAlignment = NSTextAlignmentRight;
        _lab.font = [UIFont systemFontOfSize:0.5 * self.frame.size.height];
        _lab.clipsToBounds = YES;
        [self addSubview:_lab];
    }
    return _lab;
}

#pragma mark--

//设置进度条的值

- (void)setProgress:(CGFloat)value {
    _progress = value;
    if (self.progress < 0.11) {
        UIColor *color = [UIColor colorWithRed:255 / 255.0 green:77 / 255.0 blue:58 / 255.0 alpha:1];

        _progressView.backgroundColor = color;
        _lab.textColor = color;
        self.superview.layer.borderColor = color.CGColor;
        self.superview.backgroundColor = UIColor.whiteColor;
    } else if (self.progress >= 0.11 && self.progress < 0.21) {
        UIColor *color = [UIColor colorWithRed:255 / 255.0 green:146 / 255.0 blue:45 / 255.0 alpha:1];

        _progressView.backgroundColor = color;
        _lab.textColor = color;
        self.superview.layer.borderColor = color.CGColor;
        self.superview.backgroundColor = UIColor.whiteColor;
    } else if (self.progress >= 0.21) {
        UIColor *color = [UIColor colorWithRed:6 / 255.0 green:190 / 255.0 blue:83 / 255.0 alpha:1];

        _progressView.backgroundColor = color;
        _lab.textColor = UIColor.whiteColor;
        self.superview.layer.borderColor = color.CGColor;
        self.superview.backgroundColor = [UIColor colorWithRed:6 / 255.0 green:190 / 255.0 blue:83 / 255.0 alpha:0.2];
    }
    _lab.text = [NSString stringWithFormat:@"%d%%", (int)(self.progress * 100)];
    [self changeProgressViewFrame];
}

//修改显示内容

- (void)changeProgressViewFrame {
    _progressView.frame = CGRectMake(self.frame.size.width * self.progress - self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
}

@end

@interface CTWBProgress () {
    progressSubView *subView;
}
@end

@implementation CTWBProgress

//当你通过代码创建控件就会调用这个方法
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupWithFrame:frame];
    }
    return self;
}

//当你通过storyboared或者xib中创建控件就会调用这个方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupWithFrame:self.frame];
    }
    return self;
}

- (instancetype)setupWithFrame:(CGRect)frame {
    self.frame = frame;

    //float borderWidth = 2;
    float width = frame.size.width - progressBorderWidth * 2;
    float height = frame.size.height - progressBorderWidth * 2;
    CGRect rect = CGRectMake(progressBorderWidth, progressBorderWidth, width, height);

    subView = [[progressSubView alloc] initWithFrame:rect];
    [self addSubview:subView];

    self.backgroundColor = [UIColor clearColor];
    self.layer.borderWidth = progressBorderWidth;
    self.layer.borderColor = UIColor.lightGrayColor.CGColor;
    self.layer.cornerRadius = self.frame.size.height / 2;
    self.layer.masksToBounds = YES;

    return self;
}

//重写 setter 方法，将值传递给 progressSubView
- (void)setTrackView:(UIImageView *)trackView {
    _trackView = trackView;
    subView.trackView = trackView;
}

- (void)setProgressView:(UIImageView *)progressView {
    _progressView = progressView;
    subView.progressView = progressView;
}

- (void)setLab:(UILabel *)lab {
    _lab = lab;
    subView.lab = lab;
}

- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    subView.progress = progress;
    [subView setProgress:progress];
}

//重写 getter 方法，从 progressSubView 获取值
- (UIImageView *)getTrackView {
    return subView.trackView;
}

- (UIImageView *)getProgressView {
    return subView.progressView;
}

- (UILabel *)getLab {
    return subView.lab;
}

- (CGFloat)getProgress {
    return subView.progress;
}

@end
